package edu.nf.exam.controller;

import com.github.pagehelper.PageInfo;
import edu.nf.exam.entity.ExamPaperInfo;
import edu.nf.exam.service.ExamPaperService;
import edu.nf.exam.service.impl.ExamPaperServiceImpl;
import edu.nf.exam.vo.ResultVO;
import org.nf.mvc.annotation.RequestMapping;
import org.nf.mvc.servlet.View;
import org.nf.mvc.view.JsonView;

import java.util.List;

/**
 * @Author : QYM
 * @create 2021/6/24
 */
public class ExamPaperController extends BaseController{
    /**
     * 添加试卷信息
     * @param examPaperInfo
     * @return
     */
    @RequestMapping("/examPaper/add")
    public View addExamPaper(ExamPaperInfo examPaperInfo){
        ExamPaperService service = new ExamPaperServiceImpl();
        try {
            service.checkAddExamPaper(examPaperInfo);
            return new JsonView(success("提交成功"));
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonView(fail(500,e.getMessage()));
        }
    }

    /**
     * 修改试卷信息
     * @param examPaperInfo
     * @return
     */
    @RequestMapping("/examPaper/update")
    public View updateExamPaper(ExamPaperInfo examPaperInfo){
        ExamPaperService service = new ExamPaperServiceImpl();
        try {
            service.checkUpdateExamPaper(examPaperInfo);
            return new JsonView(success());
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonView(fail(500,e.getMessage()));
        }
    }

    /**
     * 分页查询
     * @param examPaperName
     * @param paperType
     * @param levelName
     * @param subjectName
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping("/examPaper/queryByTerm")
    public View queryByTerm(String examPaperName,Integer paperType,String levelName,String subjectName,Integer page, Integer limit){
        ExamPaperService service = new ExamPaperServiceImpl();
        PageInfo<ExamPaperInfo> pageInfo = service.checkQueryByTerm(examPaperName,paperType, levelName, subjectName, page,limit);
        ResultVO<List<ExamPaperInfo>> vo = page(pageInfo);
        JsonView view = new JsonView(vo);
        view.setDateFormat("yyyy-MM-dd HH:mm:ss");
        return view;
    }

    /**
     * 根据ID查询
     * @param examPaperId
     * @return
     */
    @RequestMapping("/examPaper/queryById")
    public View queryById(Integer examPaperId){
        ExamPaperService service = new ExamPaperServiceImpl();
        try {
            ExamPaperInfo exam = service.checkQueryById(examPaperId);

            JsonView jsonView = new JsonView(success(exam));
            jsonView.setDateFormat("yyyy-MM-dd HH:mm:ss");
            return jsonView;
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonView(fail(500,e.getMessage()));
        }
    }

    /**
     * 多条件查询
     * @param examPaperInfo
     * @return
     */
    @RequestMapping("/examPaper/select")
    public View select(ExamPaperInfo examPaperInfo){
        ExamPaperService service = new ExamPaperServiceImpl();
        try {
            List<ExamPaperInfo> list = service.select(examPaperInfo);

            JsonView view = new JsonView(success(list));
            view.setDateFormat("yyyy-MM-dd HH:mm:ss");
            return view;
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonView(fail(500,e.getMessage()));
        }

    }
}
